package com.huas.controller;


import com.huas.common.PageResult;
import com.huas.common.Result;
import com.huas.dto.TalkDTO;
import com.huas.entity.Talk;
import com.huas.service.TalkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/talkManagement")
public class TalkController {

    @Autowired
    private TalkService talkService;

    /**
     * 查看说说列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('talkManagement:select')")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   Integer status) {
        if(status != null){
            log.info("查看状态为({})的说说列表,",status == 1 ? "公开":"私有");
        }else {
            log.info("查看全部说说列表");
        }
        return Result.success(talkService.page(page,pageSize,status));
    }
    /**
     * 用户查询说说列表
     */
    @GetMapping("/user/select")
    @PreAuthorize("hasAuthority('talkManagement:user:select')")
    public Result<List<Talk>> selectTalkList(){
        log.info("用户查询说说列表集合");
        List<Talk> talkList = talkService.selectTalkList();
        return Result.success(talkList);
    }
    /**
     * 发布说说
     */
    @PostMapping
    @PreAuthorize("hasAuthority('talkManagement:insert')")
    public Result<?> insert(@RequestBody Talk talk){
        log.info("发布的说说信息为：{}", talk);
        talkService.insert(talk);
        return Result.success();
    }
    /**
     * 修改说说（状态）
     */
    @PutMapping()
    @PreAuthorize("hasAuthority('talkManagement:update')")
    public Result<?> UpdateTalk(@RequestBody TalkDTO talkDTO) {
        log.info("要修改的说说信息为：{}",talkDTO);
        talkService.updateTalk(talkDTO);
        return Result.success();
    }
    /**
     * 删除说说
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('talkManagement:delete')")
    public Result<?> delete(@PathVariable Integer id){
        log.info("要删除的文章id为：{}",id);
        talkService.delete(id);
        return Result.success();
    }
    /**
     * 批量删除说说
     */
    @DeleteMapping()
    @PreAuthorize("hasAuthority('talkManagement:delete')")
    public Result<?> deleteBatch(@RequestParam("ids") List<Integer> ids){
        log.info("要批量删除的文章id为：{}",ids);
        talkService.deleteBatch(ids);
        return Result.success();
    }

}
